package com.welson.restfultoolx.settings;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * RestfulToolX插件设置状态管理
 * 负责持久化插件配置
 */
@Service
@State(
    name = "RestfulToolXSettings",
    storages = @Storage("restfulToolXSettings.xml")
)
public final class RestfulToolXSettings implements PersistentStateComponent<RestfulToolXSettings> {
    
    // 缓存设置
    public boolean enableCache = true;
    public int maxCacheSize = 1000;
    
    // 搜索设置
    public int maxSearchResults = 50;
    public boolean caseSensitiveSearch = false;
    public boolean includeDeprecated = true;
    
    // 自动刷新设置
    public boolean autoRefresh = false;
    public int refreshInterval = 30; // 秒
    
    // 界面设置
    public boolean showHttpMethodIcons = true;
    public boolean showParameterDetails = true;
    public boolean enableKeyboardNavigation = true;
    
    // 性能设置
    public int scanTimeout = 30; // 秒
    public boolean enableAsyncScanning = true;
    public boolean enableIndexOptimization = true;
    
    public static RestfulToolXSettings getInstance() {
        return ApplicationManager.getApplication().getService(RestfulToolXSettings.class);
    }
    
    @Override
    public @Nullable RestfulToolXSettings getState() {
        return this;
    }
    
    @Override
    public void loadState(@NotNull RestfulToolXSettings state) {
        XmlSerializerUtil.copyBean(state, this);
    }
    
    // Getters and Setters
    public boolean isEnableCache() {
        return enableCache;
    }
    
    public void setEnableCache(boolean enableCache) {
        this.enableCache = enableCache;
    }
    
    public int getMaxCacheSize() {
        return maxCacheSize;
    }
    
    public void setMaxCacheSize(int maxCacheSize) {
        this.maxCacheSize = Math.max(100, Math.min(maxCacheSize, 10000));
    }
    
    public int getMaxSearchResults() {
        return maxSearchResults;
    }
    
    public void setMaxSearchResults(int maxSearchResults) {
        this.maxSearchResults = Math.max(10, Math.min(maxSearchResults, 1000));
    }
    
    public boolean isCaseSensitiveSearch() {
        return caseSensitiveSearch;
    }
    
    public void setCaseSensitiveSearch(boolean caseSensitiveSearch) {
        this.caseSensitiveSearch = caseSensitiveSearch;
    }
    
    public boolean isIncludeDeprecated() {
        return includeDeprecated;
    }
    
    public void setIncludeDeprecated(boolean includeDeprecated) {
        this.includeDeprecated = includeDeprecated;
    }
    
    public boolean isAutoRefresh() {
        return autoRefresh;
    }
    
    public void setAutoRefresh(boolean autoRefresh) {
        this.autoRefresh = autoRefresh;
    }
    
    public int getRefreshInterval() {
        return refreshInterval;
    }
    
    public void setRefreshInterval(int refreshInterval) {
        this.refreshInterval = Math.max(5, Math.min(refreshInterval, 300));
    }
    
    public boolean isShowHttpMethodIcons() {
        return showHttpMethodIcons;
    }
    
    public void setShowHttpMethodIcons(boolean showHttpMethodIcons) {
        this.showHttpMethodIcons = showHttpMethodIcons;
    }
    
    public boolean isShowParameterDetails() {
        return showParameterDetails;
    }
    
    public void setShowParameterDetails(boolean showParameterDetails) {
        this.showParameterDetails = showParameterDetails;
    }
    
    public boolean isEnableKeyboardNavigation() {
        return enableKeyboardNavigation;
    }
    
    public void setEnableKeyboardNavigation(boolean enableKeyboardNavigation) {
        this.enableKeyboardNavigation = enableKeyboardNavigation;
    }
    
    public int getScanTimeout() {
        return scanTimeout;
    }
    
    public void setScanTimeout(int scanTimeout) {
        this.scanTimeout = Math.max(10, Math.min(scanTimeout, 120));
    }
    
    public boolean isEnableAsyncScanning() {
        return enableAsyncScanning;
    }
    
    public void setEnableAsyncScanning(boolean enableAsyncScanning) {
        this.enableAsyncScanning = enableAsyncScanning;
    }
    
    public boolean isEnableIndexOptimization() {
        return enableIndexOptimization;
    }
    
    public void setEnableIndexOptimization(boolean enableIndexOptimization) {
        this.enableIndexOptimization = enableIndexOptimization;
    }
    
    /**
     * 重置为默认设置
     */
    public void resetToDefaults() {
        enableCache = true;
        maxCacheSize = 1000;
        maxSearchResults = 50;
        caseSensitiveSearch = false;
        includeDeprecated = true;
        autoRefresh = false;
        refreshInterval = 30;
        showHttpMethodIcons = true;
        showParameterDetails = true;
        enableKeyboardNavigation = true;
        scanTimeout = 30;
        enableAsyncScanning = true;
        enableIndexOptimization = true;
    }
    
    /**
     * 获取设置的调试信息
     */
    public String getDebugInfo() {
        StringBuilder sb = new StringBuilder();
        sb.append("RestfulToolX Settings:\n");
        sb.append("  Cache: enabled=").append(enableCache).append(", maxSize=").append(maxCacheSize).append("\n");
        sb.append("  Search: maxResults=").append(maxSearchResults).append(", caseSensitive=").append(caseSensitiveSearch).append("\n");
        sb.append("  AutoRefresh: enabled=").append(autoRefresh).append(", interval=").append(refreshInterval).append("s\n");
        sb.append("  Performance: scanTimeout=").append(scanTimeout).append("s, asyncScanning=").append(enableAsyncScanning).append("\n");
        return sb.toString();
    }
}